<template>
    <div>
        <!--面包屑导航区域-->
        <el-breadcrumb separator="/">
            <el-breadcrumb-item :to="{ path: '/manufacturerIndex' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>订单管理</el-breadcrumb-item>
        </el-breadcrumb>

        <!--卡片视图区域-->
        <el-card>
            <!--搜索与添加区域-->
            <el-row :gutter="20">
                <el-col :span="7">
                    <el-input placeholder="请输入查询信息" v-model="queryInfo.query" clearable
                              @clear="getOrdersList">
                        <el-button slot="append" icon="el-icon-search" @click="getOrdersList"></el-button>
                    </el-input>
                </el-col>
            </el-row>

            <!--采购列表区域-->
            <el-table :data="ordersList" border stripe>
                <el-table-column type="index"></el-table-column>
                <el-table-column label="货物名称">
                    <template slot-scope="scope">
                        <el-tag size="medium">{{ scope.row.goodsName }}</el-tag>
                    </template>
                </el-table-column>
                <el-table-column label="货物数量" prop="goodsNum"></el-table-column>
                <el-table-column label="总价" prop="totalPrice"></el-table-column>
                <el-table-column label="货物状态" prop="status"
                                 :filters="[{ text: '待发货', value: '待发货' }, { text: '运输中', value: '运输中' }, { text: '已签收', value: '已签收' }]"
                                 :filter-method="filterStatus">
                    <template slot-scope="scope">
                        <el-tag type="warning" v-if="scope.row.status === '退货运输中'">退货运输中</el-tag>
                        <el-tag type="danger" v-if="scope.row.status === '待发货'">待发货</el-tag>
                        <el-tag type="success" v-if="scope.row.status === '已签收'">已签收</el-tag>
                        <el-tag type="danger" v-if="scope.row.status === '申请退货'">申请退货</el-tag>
                        <el-tag v-if="scope.row.status === '同意退货'">同意退货</el-tag>
                        <el-tag type="warning" v-if="scope.row.status === '运输中'">运输中</el-tag>
                    </template>
                </el-table-column>
                <el-table-column label="采购员名称">
                    <template slot-scope="scope">
                        <el-tag size="medium">{{ scope.row.buyerName }}</el-tag>
                    </template>
                </el-table-column>
                <el-table-column label="采购员联系方式" prop="buyerContact"></el-table-column>
                <el-table-column label="发货地址" prop="buyerAddress" :show-overflow-tooltip='true'></el-table-column>
                <el-table-column label="下单时间" prop="createTime" :show-overflow-tooltip='true'>
                    <template slot-scope="scope">
                        {{scope.row.createTime | dateFormat}}
                    </template>
                </el-table-column>
                <el-table-column label="更新时间" prop="updateTime" :show-overflow-tooltip='true'>
                    <template slot-scope="scope">
                        {{scope.row.updateTime | dateFormat}}
                    </template>
                </el-table-column>
                <el-table-column label="操作">
                    <template slot-scope="scope">
                        <el-tooltip effect="dark" content="发货" placement="top" :enterable="false">
                            <el-button type="danger" size="mini" icon="el-icon-setting"
                                       v-if="scope.row.status === '待发货'"
                                       @click="delivery(scope.row.ordersId)"></el-button>
                        </el-tooltip>
                        <el-tooltip effect="dark" content="同意退货申请" placement="top" :enterable="false">
                            <el-button type="danger" size="mini" icon="el-icon-success"
                                       v-if="scope.row.status === '申请退货'"
                                       @click="agreeReturnGoods(scope.row.ordersId)"></el-button>
                        </el-tooltip>
                        <el-tooltip effect="dark" content="收到退货" placement="top" :enterable="false">
                            <el-button type="danger" size="mini" icon="el-icon-success"
                                       v-if="scope.row.status === '退货运输中'"
                                       @click="receiveReturnGoods(scope.row.ordersId)"></el-button>
                        </el-tooltip>
                    </template>
                </el-table-column>
            </el-table>
        </el-card>
    </div>
</template>

<script scoped>
    export default {
        data() {
            return {
                queryInfo: {
                    manufacturerId: '',
                    query: '',
                    // 当前的页数
                    current: 1,
                    // 当前每页显示多少条数据
                    pageSize: 20
                },
                ordersList: [],
                total: 0,
                token: '',
            }
        },
        async created() {
            this.token = window.sessionStorage.getItem("token")
            var manufacturer = await this.$http.get('/user/manufacturer/getInformationFromToken', {
                params: {
                    token: this.token
                }
            })
            this.queryInfo.manufacturerId = manufacturer.data.data.manufacturerId
            this.getOrdersList()
        },
        methods: {
            async getOrdersList() {
                var result = await this.$http.get('orders/selectOrdersByManufacturerId', {params: this.queryInfo})
                if (result.data.status !== 200) {
                    return this.$message.error("获取订单信息列表失败！")
                }
                this.ordersList = result.data.data.records
                this.total = result.data.data.total
            },
            async delivery(ordersId) {
                //弹框是否删除
                const confirmResult = await this.$confirm('确定发货, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).catch(error => error)
                // 如果用户确认删除，则返回值为字符串 confirm
                // 如果用户取消了删除，则返回值为字符串 cancel
                if (confirmResult !== 'confirm') {
                    return this.$message.info('已取消发货')
                }
                var result = await this.$http.get('orders/update', {
                    params: {
                        orderId: ordersId,
                        status: '运输中'
                    }
                })
                if (result.data.status !== 200) {
                    return this.$message.error("修改订单状态失败！")
                }
                this.$message.success("修改订单状态成功！")
                this.getOrdersList()
            },
            filterStatus(value, row) {
                return row.status === value;
            },
            async agreeReturnGoods(ordersId) {
                //弹框是否删除
                const confirmResult = await this.$confirm('是否同意退货申请?', '提示', {
                    confirmButtonText: '同意',
                    cancelButtonText: '拒绝',
                    type: 'warning'
                }).catch(error => error)
                if (confirmResult !== 'confirm') {
                    return this.$message.info('已取消操作')
                }
                var result = await this.$http.get('orders/returnGoods', {
                    params: {
                        orderId: ordersId,
                        status: '同意退货'
                    }
                })
                if (result.data.status !== 200) {
                    return this.$message.error("取消退货申请失败！")
                }
                this.$message.success("取消退货申请成功！")
                this.getOrdersList()
            },
            async receiveReturnGoods(ordersId) {
                //弹框是否删除
                const confirmResult = await this.$confirm('确认是否收到买家发的退货?', '提示', {
                    confirmButtonText: '同意',
                    cancelButtonText: '拒绝',
                    type: 'warning'
                }).catch(error => error)
                if (confirmResult !== 'confirm') {
                    return this.$message.info('已取消操作')
                }
                var result = await this.$http.delete('orders/receiveReturnGoods', {
                    params: {
                        orderId: ordersId
                    }
                })
                if (result.data.status !== 200) {
                    return this.$message.error("操作失败！")
                }
                this.$message.success("操作成功！")
                this.getOrdersList()
            }
        }
    }
</script>

<style scoped>
    .el-breadcrumb {
        margin-bottom: 15px;
        font-size: 12px;
    }

    .el-card {
        box-shadow: 0 1px 1px rgba(0, 0, 0, 0.15) !important;
    }

    .el-table {
        margin-top: 15px;
        font-size: 12px;
    }

    .el-pagination {
        text-align: right;
        margin-top: 15px;
    }

</style>
